#php action script/usr/bin/python

import gdata.photos, gdata.photos.service
import sys, os, getopt, getpass


##########################################################
#   Client class:
#
#   Initializes with an email address for the user to log into the 
# gdata photo service.  
#
#    Data Members:
#  self.email - the email address to be used in the gdata service
#  self.client - the PhotoService client object used to interact
#    with the Picasa web service
#
#    Member Functions:
#  login() - prompts for a password, and authenticates the client object
#   
###########################################################

class Client:
	def __init__(self, email_addr):
		self.email = email_addr
		self.client = gdata.photos.service.PhotosService()	

	def login(self):
		_pass = getpass.getpass()
	
		success = 0
		retry = 0
		while not success and not retry == 3:
			try:
				# Authenticate using Google email and password.
				self.client.ClientLogin(self.email, _pass)	
				success = 1
			except gdata.service.BadAuthentication:
				print 'Login Error. Try again.'
				_pass = getpass.getpass()
				retry += 1

		if not success:
			print 'Login unsuccessful. Goodbye'
			sys.exit(1)

		print("Logged in: %s" % self.email)
	
	def autologin(self, _password):
		try:
			self.client.ClientLogin(self.email, _password)
		except gdata.service.BadAuthentication:
			sys.exit(1)

	def GetFeed(self, feeduri):
		return self.client.GetFeed(feeduri)

	def InsertAlbum(self, albumname):
		return self.client.InsertAlbum(albumname, albumname)
	
        def InsertPhotoSimple(self, album_or_uri, title, summary, filename_or_handle, keywords):
		return self.client.InsertPhotoSimple(album_or_uri=album_or_uri, title=title, \
			summary=summary, filename_or_handle=filename_or_handle, keywords=keywords)

	def UpdatePhotoMetadata(self, photoEntry):
		return self.client.UpdatePhotoMetadata(photoEntry)

	def InsertComment(self, photoEntry, commentString):
		return self.client.InsertComment(photoEntry, commentString)
